/*
 * @Author: 毕晨曦
 * @Date: 2023-07-10 12:58:10
 * @LastEditors: lihaonan
 * @LastEditTime: 2024-06-25 21:34:45
 * @FilePath: /wucha_wechat_miniprog/src/store/user.ts
 * @Description: 用户信息store
 */
import { ref } from "vue";
import { defineStore } from "pinia";
import { getUserInfo } from "@/api/appNumber";

export const useUserStore = defineStore("userInfo", () => {
  const userInfo = ref();
  const phone = ref();
  const code = ref();
  const path = ref();

  /**
   * @description: 设置用户信息
   * @param {} param
   * @return {*}
   */
  function setUser(param: {}) {
    userInfo.value = param;
    uni.setStorage({
      key: "userInfo",
      data: param,
    });
    return userInfo.value;
  }

  /**
   * @description: 获取用户信息
   * @return {}
   */
  function getUser() {
    return userInfo.value;
  }

  // 设置手机号
  function setPhone(param: string) {
    phone.value = param;
    uni.setStorage({
      key: "phone",
      data: param,
    });
  }

  // 设置wx code码
  function setCode(param: string) {
    code.value = param;
    uni.setStorage({
      key: "code",
      data: param,
    });
  }
  // 设置基础访问路径
  function setPath(param: string) {
    path.value = param;
    uni.setStorage({
      key: "path",
      data: param,
    });
  }

  /**
   * @description: 登陆用户信息
   * @return {*}
   */
  async function useLoginUser() {
    const userData = await getUserInfo(phone.value, code.value);
    setUser(userData.data);
  }

  return {
    userInfo,
    setUser,
    getUser,
    useLoginUser,
    setPhone,
    setCode,
    setPath,
  };
});
